<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="java.util.Map"%>
<%@page import="com.feib.soeasy.util.CodeTableUtil"%>	
<c:choose>
	<c:when test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		<c:set var="lastPageNumber" value="${actionBean.queryResult.totalPageCount}"/>
		<c:set var="currentPageNumber" value="${actionBean.queryResult.pageNumber}"/>
		<c:set var="prePageNumber" value="${actionBean.queryResult.pageNumber - 1}"/>
		<c:set var="nextPageNumber" value="${actionBean.queryResult.pageNumber + 1}"/>
		
		<div id="paginationBar">
			<input type="button" id="firstPageBtn" value="第一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			<input type="button" id="prePageBtn" value="上一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			第 <input type="text" id="currentPageText" value="${currentPageNumber }" size="3" maxlength="4"> / ${lastPageNumber} 頁
			<input type="button" id="nextPageBtn" value="下一頁" <c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
			<input type="button" id="lastPageBtn" value="最末頁" <c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
		</div>
	
	<div>
		<table id="listTable">
			<thead>						
				<tr>
					<th>
					<c:choose>
						<c:when test="${actionBean.group.groupType == 2 }">學號</c:when>
						<c:otherwise>客戶編號</c:otherwise>
					</c:choose>
					</th>
					<th>繳費帳號</th>
					<th>客戶名稱</th>
					<th>異常原因</th>
					<th>繳款通路</th>					
					<th>繳款截止日</th>
					<th>繳款交易日<br/>繳款核銷日</th>
					<th>應繳金額</th>
					<th>實繳金額</th>
					<!--  
					<th>應退補差額1</th>	
					<c:if test="${actionBean.group.groupType == 2 }">
					<th>應繳金額2</th>
					<th>實繳金額2</th>
					<th>應退補差額2</th>	
					</c:if>
					-->
					<th>&nbsp;</th>			    
				</tr>
			</thead>
			<tbody>
				<c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">
				
				<tr>
					<td>
				  	<c:choose>
						<c:when test="${actionBean.group.groupType == 2 }"><c:out value="${viewObj.STUDENT_ID}"/></c:when>
						<c:otherwise><c:out value="${viewObj.PAYER_ID}"/></c:otherwise>
					</c:choose>
					</td>
					<td><c:out value="${viewObj.USERNO}"/></td>
					<td><c:out value="${viewObj.PAYER_NAME}"/></td>
					<td><c:out value="${viewObj.ERR_MSG}"/></td>
					<td>
					<%
					Map<String, Object> payment = (Map<String, Object>)pageContext.getAttribute("viewObj");
					String channel = (String)payment.get("FEEDBACK_CHANNEL");
					if (null != channel)
					{
					    if ("7111111".equalsIgnoreCase(channel))
					        channel = "D";
					    else if ("TFM".equalsIgnoreCase(channel))
					        channel = "E";
					    else if ("HILIFE".equalsIgnoreCase(channel))
					        channel = "F";
					    else if ("OKCVS".equalsIgnoreCase(channel))
					        channel = "G";
					    else if ("POST".equalsIgnoreCase(channel))
					        channel = "H";
					    else if ("ACH".equalsIgnoreCase(channel))
					        channel = "I";
					    
					}
					
					if ("I".equalsIgnoreCase(channel))
						{
					    	out.println(CodeTableUtil.getInstance().getCodeName(CodeTableUtil.CODE_KIND_CHANNEL, channel).substring(0,4));
						}else{
					 		out.println(CodeTableUtil.getInstance().getCodeName(CodeTableUtil.CODE_KIND_CHANNEL, channel));
						}
					%>
					</td>
					
					<td align="center"><fmt:formatDate value="${viewObj.PAY_DUE}" pattern="yyyy/MM/dd"  /></td>
					<fmt:parseDate var="aday" pattern="yyyyMMdd" value="${viewObj.ADAY }" />
					<td align="center"><fmt:formatDate value="${aday}" pattern="yyyy/MM/dd"  />
					<br/>
					<fmt:formatDate value="${viewObj.FBTIME}" pattern="yyyy/MM/dd"  /></td>
					
					<c:choose>
						<c:when test="${actionBean.group.groupType == 99 }">
						<%// 因為畫面欄位過長，所以只顯示應繳金額1 與時繳金額1，故讓他永遠是false，之後若要還原可以改成actionBean.group.groupType == 2  %>
							<c:choose>
								<c:when test="${viewObj.TXAMT == viewObj.TOTAL_LOANABLE_AMOUNT }">
					<td align="right"><fmt:formatNumber value="${viewObj.TOTAL_AMOUNT}" pattern="##,###" /></td>
					<td align="right">0</td>
					<td align="right">0</td>
					<td align="right"><fmt:formatNumber value="${viewObj.TOTAL_LOANABLE_AMOUNT}" pattern="##,###" /></td>
					<td align="right"><fmt:formatNumber value="${viewObj.TXAMT}" pattern="##,###" /></td>
					<td align="right"><fmt:formatNumber value="${viewObj.TXAMT - viewObj.TOTAL_LOANABLE_AMOUNT}" pattern="##,###" /></td>
								</c:when>
								<c:otherwise>
					<td align="right"><fmt:formatNumber value="${viewObj.TOTAL_AMOUNT}" pattern="##,###" /></td>
					<td align="right"><fmt:formatNumber value="${viewObj.TXAMT}" pattern="##,###" /></td>
					<td align="right"><fmt:formatNumber value="${viewObj.TXAMT - viewObj.TOTAL_AMOUNT}" pattern="##,###" /></td>
					<td align="right"><fmt:formatNumber value="${viewObj.TOTAL_LOANABLE_AMOUNT}" pattern="##,###" /></td>
					<td align="right">0</td>
					<td align="right">0</td>
								</c:otherwise>
							</c:choose>
						</c:when>
						
						<c:otherwise>
					<td align="right"><fmt:formatNumber value="${viewObj.TOTAL_AMOUNT}" pattern="##,###" /></td>
					<td align="right"><fmt:formatNumber value="${viewObj.TXAMT}" pattern="##,###" /></td>
					<!--<td align="right"><fmt:formatNumber value="${viewObj.TXAMT - viewObj.TOTAL_AMOUNT}" pattern="##,###" /></td>-->
						</c:otherwise>
					</c:choose>
					
					<td><c:if test="${viewObj.STUDENT_ID ne null ||viewObj.PAYER_ID  ne null }">
						<div id="viewFeedbackLogImg-<c:out value='${viewObj.UID}'/>"><a href="javascript: void(0)" class="btnZoom">查詢</a></div>
					</c:if>
					</td>						
				</tr>
				</c:forEach>
			</tbody>
		</table>
		</div>
		
		<div id="paginationBar">
			<input type="button" id="doDownloadExcelFileBtn" name="doDownloadExcelFileBtn"  value="人工核銷明細表(Excel)"/>
			<input type="button" id="printBtn2" value="列印" />
		</div>
		
		
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$("[id^='viewFeedbackLogImg-']").click(function() {
				var uid = this.id.substring(19);
				doQueryFeedbackLogAction(uid);
			});
			
			$("#doDownloadExcelFileBtn").click(function() {
				doDownloadExcelFileAction();
			});
			
			$('#printBtn2').click(function() {
				//window.print();
				//doQueryPrintAction();
				divPrint('queryResultDiv');//只印目前這筆
			});
			
			
			
			
			$("[id^='firstPageBtn']").click(function() {
				doQueryAction(1);
			});
			
			$("[id^='prePageBtn']").click(function() {
				doQueryAction(<c:out value='${prePageNumber}'/>);
			});
			
			$("[id^='nextPageBtn']").click(function() {
				doQueryAction(<c:out value='${nextPageNumber}'/>);
			});
			
			$("[id^='lastPageBtn']").click(function() {
				doQueryAction(<c:out value='${lastPageNumber}'/>);
			});
			
			$("[id^='currentPageText']").change(function() {
				var value = this.value;
				if (value != ''){
					value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
					var intRegex = /^\d+$/;
					if(!intRegex.test(value)) {
						alert('頁數必須是整數');
						return ;
					}
					
					value = parseInt(value, 10);
					
					if (value < 1){
						alert('頁數必須是大於1');
						return ;
					}
					else if (value > <c:out value='${lastPageNumber}'/>){
						alert('頁數必須是小於總頁數 (<c:out value='${lastPageNumber}'/>)');
						return ;
					}
					else if (value != <c:out value='${currentPageNumber}'/>){
						doQueryAction(value);
					}
					
				}
				else{
					alert('頁數不可為空白');
					return ;
				}
			});
			
		});
		//-->
		</script>
		
	</c:when>
	<c:otherwise>
	
	
	<div id="queryNothingBlockDiv" style="display: none;">
			查無資料
		</div>		
	<table id="listTable">
			<thead>						
				<tr>
					<th>
					<c:choose>
						<c:when test="${actionBean.group.groupType == 2 }">學號</c:when>
						<c:otherwise>客戶編號</c:otherwise>
					</c:choose>
					</th>
					<th>繳費帳號</th>
					<th>客戶名稱</th>
					<th>異常原因</th>
					<th>繳款通路</th>					
					<th>繳款截止日</th>
					<th>繳款交易日</th>
					<th>繳款核銷日</th>
					<th>應繳金額</th>
					<th>實繳金額</th>
					<th>&nbsp;</th>			    
				</tr>
			</thead>
		</table>	
		
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$( "#queryNothingBlockDiv" ).dialog({  title:'<%=UserUtil.getCurrentMenuItemName() %>' ,width: 300, resizable: false, modal: true, buttons : {
				Ok : function() {
					$(this).dialog("close");
				}
			}  });
		});
		//-->
		</script>			
	</c:otherwise>
</c:choose>
